<?php
// 引入数据库连接文件
require_once 'db_connection.php';

// 获取图书列表
$sql = "SELECT * FROM books";
$result = $conn->query($sql);

// 检查是否有图书记录
if ($result->num_rows > 0) {
    // 输出图书列表
    while ($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . $row["id"] . "</td>";
        echo "<td>" . $row["title"] . "</td>";
        echo "<td>" . $row["author"] . "</td>";
        echo "<td>" . $row["isbn"] . "</td>";
        echo "<td>" . $row["publishedDate"] . "</td>";
        echo "<td>";
        echo "<a href='edit_book.php?id=" . $row["id"] . "'>编辑</a> ";
        echo "<a href='delete_book.php?id=" . $row["id"] . "'>删除</a>";
        echo "</td>";
        echo "</tr>";
    }
} else {
    echo "<tr><td colspan='6'>没有图书记录</td></tr>";
}

// 关闭数据库连接
$conn->close();
?>

<!-- 添加新书的表单 -->
<form action="add_book.php" method="post">
    <input type="text" name="title" placeholder="书名" required>
    <input type="text" name="author" placeholder="作者" required>
    <input type="text" name="isbn" placeholder="ISBN" required>
    <input type="date" name="publishedDate" required>
    <button type="submit">添加新书</button>
</form>